cmake_minimum_required(VERSION 3.0.2)
project(ptz_control)

SET(CMAKE_BUILD_TYPE "Release")

# SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O0 -Wall -pthread")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall -pthread -fopenmp")
add_compile_options(-std=c++14)
add_definitions(-std=c++14)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(PkgConfig REQUIRED)
find_package(yaml-cpp REQUIRED)
find_package(catkin REQUIRED COMPONENTS
            roscpp
            rospy
            rosbag
            std_msgs
            geometry_msgs
            sensor_msgs
            nav_msgs
            message_generation
            std_srvs
)

set(ALL_TARGET_LIBRARIES "")

pkg_check_modules(YAML_CPP REQUIRED yaml-cpp)
include_directories(${YAML_CPP_INCLUDEDIR})
list(APPEND ALL_TARGET_LIBRARIES ${YAML_CPP_LIBRARIES})

include_directories(
    include ${catkin_INCLUDE_DIRS}
)

add_service_files(
  DIRECTORY srv
  FILES

  # ptz_control
  absolute_move.srv
  continuous_move.srv
  goto_preset.srv
  stop_move.srv
)

generate_messages(
  DEPENDENCIES
  std_msgs
)

catkin_package(INCLUDE_DIRS
    include
    CATKIN_DEPENDS
    std_msgs
    message_runtime
    message_generation
    roscpp)



###########
## Build ##
###########
file(GLOB_RECURSE ALL_SRCS "*.cpp")
file(GLOB_RECURSE NODE_SRCS "src/*_node.cpp")
list(REMOVE_ITEM ALL_SRCS ${NODE_SRCS})

add_executable(ptz_node src/ptz_node.cpp ${ALL_SRCS})
add_dependencies(ptz_node ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries (ptz_node ${catkin_LIBRARIES} ${ALL_TARGET_LIBRARIES})
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
	message(STATUS "CMAKE_SYSTEM_PROCESSOR x86_64")
	target_link_libraries (ptz_node
		${PROJECT_SOURCE_DIR}/lib/linux-x86_64/libOnvif_API.so
	)
elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
    message(STATUS "CMAKE_SYSTEM_PROCESSOR aarch64")	
	target_link_libraries (ptz_node
		${PROJECT_SOURCE_DIR}/lib/aarch64/libOnvif_API.so
	)
else()
    message("unknow CMAKE_SYSTEM_PROCESSOR")
endif()

#############
## Install ##
#############

## Mark executables for installation
## See http://docs.ros.org/melodic/api/catkin/html/howto/format1/building_executables.html
install(TARGETS
        ptz_node
    RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

## Mark cpp header files for installation
install(DIRECTORY 
        include/
    DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
    FILES_MATCHING PATTERN "*.hpp"
    PATTERN ".svn" EXCLUDE
)

## Mark other directories for installation:
install(DIRECTORY
        launch/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/launch
    FILES_MATCHING PATTERN "*.launch"
)

install(DIRECTORY
        script/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/script
    FILES_MATCHING PATTERN "*" 
)

install(DIRECTORY
        config/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/config
    FILES_MATCHING PATTERN "*.yaml" 
)

install(DIRECTORY
        version/
    DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}/version
    FILES_MATCHING PATTERN "*.txt"
)

#############
## Testing ##
#############

## Add gtest based cpp test target and link libraries
# catkin_add_gtest(${PROJECT_NAME}-test test/test_preprocess.cpp)
# if(TARGET ${PROJECT_NAME}-test)
#   target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME})
# endif()

## Add folders to be run by python nosetests
# catkin_add_nosetests(test)
